/**
 * 兑换商品
 */
class exGoods extends eui.Component{
    private return:eui.Button;
    private require:eui.Button;
    private userName:eui.Label;
    private phone:eui.Label;
    private detail:eui.Label;
    private exchange:eui.Label;
    private list:eui.List;
    private _arrayData:eui.ArrayCollection;
    private _exData:Array<any> = [];
    private _addressListUI:addressList;
    private _addressObj:any = {};
    public constructor(arr){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "exGoodsSkin"; 
        this.valueGoods(arr);
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        Dispatcher.ins.addEventListener("addressList", this.getDefaultAddress, this);
        Dispatcher.ins.addEventListener("exGoods", this.exGoods, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this,true);
        this.require.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let node = [];
            for(var i = 0,lg = this._exData.length;i < lg;i++){
                let obj = this._exData[i];
                let trObj = {
                    "propId":obj.PropId,
                    "propNum":parseInt(obj.num.substr(1)),
                }
                node.push(trObj);
            }
            let dataPost = {
                "aId":this._addressObj.Id,
                "deliverData":node
            }
            socket.request("exGoods",dataPost);
        },this);
        this.exchange.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(!this._addressListUI){
                this._addressListUI = new addressList(true);
                this._addressListUI.addEventListener("changeDetail",this.changeDetail,this);
            }
            this.addChild(this._addressListUI);
        },this);
    }
    public getList():void{
        let dataPost = {"node":0};
        socket.request("addressList",dataPost,0);
    }
    public valueGoods(arr):void{
        this._exData = arr;
        this._arrayData =  new eui.ArrayCollection(this._exData);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = exGoodsIR;
        this.getList();
    }
    public getDefaultAddress(e: GameEvent):void{
        let obj = e.data;
        if(obj.note !== 0) return;
        if(obj.Address.length == 0){
            let toast = new Toast();
            toast.show("请先至物流中心添加地址");
            this.parent.addChild(toast);
            this.parent.removeChild(this);
        }else{
            this._addressObj = obj;
            this.userName.text = obj.UserName;
            this.phone.text = obj.PhoneNum;
            this.detail.text = obj.Province + obj.City + obj.Area + obj.Address;
        }

        // if(result.node == "exGoods"){
        //     this.dispatchEventWith("ok");
        //     this.parent.removeChild(this);
        // }
    }
    private exGoods(e:GameEvent):void{
        this.dispatchEventWith("ok");
        this.parent.removeChild(this);
    }
    private changeDetail(evt:GameEvent):void{
        let result = evt.data;
        this.removeChild(this._addressListUI);
        this.userName.text = result.UserName;
        this.phone.text = result.PhoneNum;
        this.detail.text = result.detailed;
        this._addressObj = result;
    }
}
class exGoodsIR extends eui.ItemRenderer{
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "exGoodsIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
    
    }
    protected dataChanged():void{
        
    }
} 